import {defineStore} from "pinia"
import {http} from "../axios/index.js"

export const useWidgetStore = defineStore("widget",{
    actions: {
        //设置小部件
         setWidget(model){
            window.L2Dwidget.init({
                pluginRootPath: '../public/live2dw/', // 开发版
                // pluginRootPath: './live2dw/', // 打包版
                pluginJsPath: 'lib/',
                tagMode: false,
                debug: false,
                //模型
                pluginModelPath: `live2d-widget-model-${model.path}/assets/`,
                model: {
                    jsonPath: `../public/live2dw/live2d-widget-model-${model.path}/assets/${model.model}.model.json` //开发版
                    // jsonPath: `./live2dw/live2d-widget-model-${model.path}/assets/${model.model}.model.json` //打包版
                },
                //调整大小,和位置
                display: {
                    position: 'right',
                    width: window.innerWidth > 800 ? 175 : 87.5,
                    height: window.innerWidth > 800 ? 400 : 200,
                    vOffset: 0
                }
            })
        },
        //切换
        async toggleWidget(id){
            const res = await http({
                method: "GET",
                url: "widget/get_widget",
                params: {
                    wid: id
                }
            })
            let model = {}
            if(res.data.code === 0){
                model = res.data.data.widgetList[0]
            }else{
                model = {
                    path: "haru_1",
                    model: "haru01"
                }
            }
            //先删除现有的
            const live2dWidget = window.document.getElementById("live2d-widget")
            live2dWidget.remove()
            //然后把路径更改为新模型路径
            window.L2Dwidget.config.model.jsonPath = `../public/live2dw/live2d-widget-model-${model.path}/assets/${model.model}.model.json` //开发版
            // window.L2Dwidget.config.model.jsonPath = `./live2dw/live2d-widget-model-${model.path}/assets/${model.model}.model.json` //打包版
            window.L2Dwidget.config.pluginModelPath = `live2d-widget-model-${model.path}/assets/`
            //最后重新渲染出来
            this.setWidget(model)
        }
    }
})